<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface
//我的提现账户
class bank extends core {

	public function __construct() {
		parent::__construct();
	}

	public function index() {
		$sql = "SELECT * FROM {$this->tbp}member_bank WHERE member_id='{$this->member_id}' ORDER BY id ASC";
		$banks = $this->db->get_results($sql);
		success($banks);
	}

	public function add() {
		if (IS_POST) {
			$this->save();
		} else {
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			if ($id>0) {
				$this->edit();
			} else {
				$rs = new stdClass();
				$rs->id = 0;
				$rs->bank_name = '';
				$rs->address = '';
				$rs->branch_name = '';
				$rs->name = '';
				$rs->card_number = '';
				success($rs);
			}
		}
	}

	public function edit() {
		if (IS_POST) {
			$this->save();
		} else {
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}member_bank WHERE member_id='{$this->member_id}' AND id='{$id}'";
			$rs = $this->db->get_row($sql);
			success($rs);
		}
	}

	public function save() {
		$id = (isset($_POST['id']) && trim($_POST['id'])) ? intval($_POST['id']) : 0;
		$bank_name = (isset($_POST['bank_name']) && trim($_POST['bank_name'])) ? trim($_POST['bank_name']) : '';
		$address = (isset($_POST['address']) && trim($_POST['address'])) ? trim($_POST['address']) : '';
		$branch_name = (isset($_POST['branch_name']) && trim($_POST['branch_name'])) ? trim($_POST['branch_name']) : '';
		$name = (isset($_POST['name']) && trim($_POST['name'])) ? trim($_POST['name']) : '';
		$card_number = (isset($_POST['card_number']) && trim($_POST['card_number'])) ? trim($_POST['card_number']) : '';
		if ($id) {
			$sql = "UPDATE {$this->tbp}member_bank SET bank_name='{$bank_name}', address='{$address}', branch_name='{$branch_name}', 
				name='{$name}', card_number='{$card_number}' 
				WHERE member_id='{$this->member_id}' AND id='{$id}'";
			$this->db->query($sql);
		} else {
			$sql = "INSERT INTO {$this->tbp}member_bank(member_id, bank_name, address, branch_name, name, card_number, add_time)
				VALUES('{$this->member_id}', '{$bank_name}', '{$address}', '{$branch_name}', '{$name}', '{$card_number}', '".time()."')";
			$this->db->query($sql);
			$id = $this->db->insert_id;
			//检测是否已设置提现密码
			$sql = "SELECT count(*) FROM {$this->tbp}member WHERE id='{$this->member_id}' AND withdraw_password!=''";
			if (!$this->db->get_var($sql)) {
				$_POST['gourl'] = 'wap.php?app=bank&act=password_set';
				$_POST['goalert'] = '请设置提现密码';
			}
		}
		$sql = "SELECT * FROM {$this->tbp}member_bank WHERE member_id='{$this->member_id}' AND id='{$id}'";
		$banks = $this->db->get_row($sql);
		success($banks);
	}

	public function delete() {
		$id = (isset($_POST['id']) && trim($_POST['id'])) ? intval($_POST['id']) : 0;
		$sql = "DELETE FROM {$this->tbp}member_bank WHERE id='{$id}' AND member_id='{$this->member_id}'";
		$this->db->query($sql);
		success("ok");
	}

    public function set(){
        success("ok");
    }
	
	//设置密码
	public function password_set(){
		if (IS_POST) {
			$password = $this->request->post('password');
			if ($password=='') error('提现密码不能为空');
			$salt = generate_salt();
			$crypt_password = crypt_password($password, $salt);
			$sql = "UPDATE {$this->tbp}member SET withdraw_password='{$crypt_password}', withdraw_salt='{$salt}' WHERE id='{$this->member_id}'";
			$this->db->query($sql);
			$_SESSION['member']->withdraw_password = $crypt_password;
			success('ok');
		} else {
	    	success("ok");
		}
    }
	
	public function password_edit() {
		if (IS_POST) {
			$password = $this->request->post('password');
			if ($password=='') error('提现密码不能为空');
			$sql = "SELECT withdraw_password, withdraw_salt FROM {$this->tbp}member WHERE id='{$this->member_id}'";
			$row = $this->db->get_row($sql);
			$crypt_password = crypt_password($password, $row->withdraw_salt);
			if ($row->withdraw_password!=$crypt_password) error('提现密码不正确');
			success('ok');
		} else {
	    	success("ok");
		}
	}
	
    public function forget(){
	    if (IS_POST) {
			$mobile = $this->request->post('mobile');
			$code = $this->request->post('code');
			$forget_sms_code = $this->request->session('forget_sms_code');
			if ($mobile=='' || $code=='') error('请输入手机号码与验证码');
			if (!$forget_sms_code || $code!=$forget_sms_code) error('验证码不正确');
			$sql = "SELECT count(*) FROM {$this->tbp}member WHERE id='{$this->member_id}' AND mobile='{$mobile}'";
			if (!$this->db->get_var($sql)) error('无此会员');
			success('ok');
		} else {
	    	success("ok");
		}
    }

	public function repassword(){
	    success("ok");
    }
}
